package com.zhudeshuai.zhudemolist.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhu.zhuCore.utils.EmptyUtil;
import com.zhu.zhuCore.utils.ToastUtil;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.bean.realm_bean.Question;

import java.util.List;

/**
 * @author zhudeshuai
 * @since 2018/10/13
 */

public class AnswerQuestionDialog extends Dialog {

	private TextView mProgressTv;
	private TextView mTitleTv;
	private LinearLayout mSelectItemsLl;
	private Button mSureBtn;
	private Button mNextBtn;

	private CheckBox mSelectA;
	private CheckBox mSelectB;
	private CheckBox mSelectC;
	private CheckBox mSelectD;

	private List<Question> mData;

	private int mCur;

	private Question mCurQuestion;

	public AnswerQuestionDialog(@NonNull Context context) {
		super(context, R.style.dialogNoTitle);
		setContentView(R.layout.dialog_answer_question);
		initView();
	}

	private void initView() {
		mProgressTv = (TextView) findViewById(R.id.tv_progress);
		mTitleTv = (TextView) findViewById(R.id.tv_title);
		mSelectItemsLl = (LinearLayout) findViewById(R.id.ll_select_items);
		mSureBtn = (Button) findViewById(R.id.btn_sure);
		mNextBtn = (Button) findViewById(R.id.btn_next);

		mSelectA = (CheckBox) findViewById(R.id.cb_a);
		mSelectB = (CheckBox) findViewById(R.id.cb_b);
		mSelectC = (CheckBox) findViewById(R.id.cb_c);
		mSelectD = (CheckBox) findViewById(R.id.cb_d);

		mSureBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//检查答案是否正确
				boolean selectedA = mSelectA.isChecked();
				boolean selectedB = mSelectB.isChecked();
				boolean selectedC = mSelectC.isChecked();
				boolean selectedD = mSelectD.isChecked();

				StringBuilder sb = new StringBuilder();
				if (selectedA) {
					sb.append("A");
				}
				if (selectedB) {
					sb.append("B");
				}
				if (selectedC) {
					sb.append("C");
				}
				if (selectedD) {
					sb.append("D");
				}
				if (sb.toString().equals(mCurQuestion.getAnswerStr().trim())) {
					ToastUtil.show("回答正确", ToastUtil.Type.SUCCESS);
				} else {
					ToastUtil.show("回答错误", ToastUtil.Type.FAILED);
				}

			}
		});

		mNextBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if (mCur >= mData.size() - 1) {
					dismiss();
				}

				mCur++;
				resetCurDataAndShow();
			}
		});

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (EmptyUtil.isEmpty(mData)) return;
		resetCurDataAndShow();

	}

	private void resetCurDataAndShow() {
		mCurQuestion = mData.get(mCur);
		mCurQuestion.preGet();
		toShowQuestion();
	}

	private void toShowQuestion() {
		mProgressTv.setText((mCur + 1) + "/" + mData.size());
		mTitleTv.setText(mCurQuestion.getQuestionDes());

		if (mCurQuestion.selectItems.size() > 3) {
			mSelectA.setChecked(false);
			mSelectB.setChecked(false);
			mSelectC.setChecked(false);
			mSelectD.setChecked(false);
			mSelectA.setText("A." + mCurQuestion.selectItems.get("A"));
			mSelectB.setText("B." + mCurQuestion.selectItems.get("B"));
			mSelectC.setText("C." + mCurQuestion.selectItems.get("C"));
			mSelectD.setText("D." + mCurQuestion.selectItems.get("D"));
		}

	}

	public void setData(List<Question> all) {
		this.mData = all;
	}
}
